package ru.mirent.libs.filter;

import ru.mirent.utils.RegexpMatcher;

import java.util.List;

public class SimpleRegexpListFilter implements DataFilter {
    private List<String> list;
    private StrategyFilter strategyFilter;
    private boolean defaultResult;

    public SimpleRegexpListFilter(List<String> list, StrategyFilter strategyFilter, boolean defaultResult) {
        this.list = list;
        this.strategyFilter = strategyFilter;
        this.defaultResult = defaultResult;
    }

    public SimpleRegexpListFilter(List<String> list, StrategyFilter strategyFilter) {
        this(list, strategyFilter, true);
    }

    @Override
    public boolean filter(String text) {
        boolean result = strategyFilter.isValue();
        for (String regexp: list) {
            result = new RegexpMatcher().apply(text, regexp) == strategyFilter.isValue();
            if (StrategyFilter.INCLUDE.equals(strategyFilter)) {
                if (result) {
                    return result;
                }
            } else if (StrategyFilter.EXCLUDE.equals(strategyFilter)) {
                if (!result) {
                    return result;
                }
            }
        }

        return defaultResult;
    }
}
